package cn.ecnu.note.controller;


import cn.ecnu.note.service.NoteService;
import cn.ecnu.note.vo.NoteFrontVo;
import cn.ecnu.note.vo.NoteVo;
import cn.ecnu.note.vo.ViewNoteVo;
import cn.ecnu.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/note")
public class NoteController {

    @Autowired
    private NoteService noteService;

    // 新增Note
    // TODO: @Own(permission = Permission.POST_NOTE)
    @PostMapping("/add")
    public R addNote(@RequestBody NoteFrontVo vo) {
        NoteVo noteVo = noteService.addNote(vo);
        return R.ok().put("data", noteVo);
    }

    // 删除Note
    // TODO: @Own(permission = Permission.VIEW)
    @GetMapping("/delete/{noteId}")
    public R deleteNote(@PathVariable("noteId") Integer noteId,
                        @RequestHeader("userId") Integer userId,
                        @RequestHeader("permission") Integer permission) {
        noteService.deleteNoteById(noteId, userId, permission);
        return R.ok();
    }

    // 根据paperId来查Note
    //@Own(permission = Permission.VIEW)
    @GetMapping("/getNoteByPaperId/{paperId}")
    public R getNoteByPaperId(@PathVariable("paperId") Integer paperId,
                              @RequestParam(value = "pageNum", required = false) Integer pageNum) {
        ViewNoteVo vo = noteService.getNoteByPaperId(paperId, pageNum);
        return R.ok().put("data", vo);
    }

    // @Own(permission = Permission.VIEW)
    @GetMapping("/getNoteByUserId/{userId}")
    public R getNoteByUserId(@PathVariable("userId") Integer userId,
                             @RequestParam(value = "pageNum", required = false) Integer pageNum) {
        ViewNoteVo vo = noteService.getNoteByUserId(userId, pageNum);
        return R.ok().put("data", vo);
    }

    // TODO: @Own(permission = Permission.VIEW)
    @GetMapping("/getNoteByNoteId/{noteId}")
    public R getNoteByNoteId(@PathVariable("noteId") Integer noteId) {
        NoteVo vo = noteService.getNoteByNoteId(noteId);
        return R.ok().put("data", vo);
    }

    // @Own(permission = Permission.VIEW)
    @PostMapping("/update")
    public R updateNoteById(@RequestBody NoteFrontVo vo,
                            @RequestHeader("userId") Integer userId,
                            @RequestHeader("permission") Integer permission) {
        noteService.updateNoteById(vo, userId, permission);
        return R.ok();
    }


}
